//
//  Utils.m
//  MemberProject
//
//  Created by MAC OS X on 15/4/3.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "Utils.h"

@implementation Utils
+ (BOOL) validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}
+ (BOOL) validateMobile:(NSString *)mobile
{
    //手机号以13， 15，18开头，八个 \d 数字字符
//    /^1[3|4|5|8][0-9]\d{4,8}$/
//    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSString *phoneRegex = @"^1[3|4|5|8][0-9]\\d{4,8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}
+(BOOL)validataDate:(NSString *)date
{
    //验证日期 格式yyyy-mm-dd
    NSString *dateRegex = @"^((\\d*(0?[048]|[2468][048]|[13579][26])00|\\d*(0?[48]|[2468][048]|[13579][26]))-0?2-29)$|^[1-9]\\d*-((0?[13578]|10|12)-(3[01]|[12]\\d|0?[1-9])|(0?[469]|11)-(30|[12]\\d|0?[1-9])|(0?2)-(2[0-8]|1\\d|0?[1-9]))$";
    NSPredicate *dateTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",dateRegex];
    return [dateTest evaluateWithObject:date];
}

+ (BOOL) validateIdCard:(NSString *)idCard{
    //验证身份证
    
    
    //    NSString *isCardStr = @"(^\\d{6}((0[48]|[2468][048]|[13579][26])0229|\\d\\d(0[13578]|10|12)(3[01]|[12]\\d|0[1-9])|(0[469]|11)(30|[12]\\d|0[1-9])|(02)(2[0-8]|1\\d|0[1-9]))\\d{3}$)|(^\\d{6}((2000|(19|21)(0[48]|[2468][048]|[13579][26]))0229|(((20|19)\\d\\d)|2100)(0[13578]|10|12)(3[01]|[12]\\d|0[1-9])|(0[469]|11)(30|[12]\\d|0[1-9])|(02)(2[0-8]|1\\d|0[1-9]))\\d{3}[\\dX]$)";
    //
    //        NSPredicate *cardTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",isCardStr];
    //        return [cardTest evaluateWithObject:idCard];
    //    }
    NSString *cardNo=idCard;
    if (cardNo.length != 18&&cardNo.length!=15) {
        return  NO;
    }
    NSArray* codeArray = [NSArray arrayWithObjects:@"7",@"9",@"10",@"5",@"8",@"4",@"2",@"1",@"6",@"3",@"7",@"9",@"10",@"5",@"8",@"4",@"2", nil];
    NSDictionary* checkCodeDic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1",@"0",@"X",@"9",@"8",@"7",@"6",@"5",@"4",@"3",@"2", nil]  forKeys:[NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil]];
    if(cardNo.length==18){
        NSScanner* scan = [NSScanner scannerWithString:[cardNo substringToIndex:17]];
        
        int val;
        BOOL isNum = [scan scanInt:&val] && [scan isAtEnd];
        if (!isNum) {
            return NO;
        }
        int sumValue = 0;
        
        for (int i =0; i<17; i++) {
            sumValue+=[[cardNo substringWithRange:NSMakeRange(i , 1) ] intValue]* [[codeArray objectAtIndex:i] intValue];
        }
        
        NSString* strlast = [checkCodeDic objectForKey:[NSString stringWithFormat:@"%d",sumValue%11]];
        
        if ([strlast isEqualToString: [[cardNo substringWithRange:NSMakeRange(17, 1)]uppercaseString]]) {
            return YES;
        }
        return  NO;
    }
    if(cardNo.length==15){
//        NSScanner* scan = [NSScanner scannerWithString:[cardNo substringToIndex:14]];
//        
//        int val;
//        BOOL isNum = [scan scanInt:&val] && [scan isAtEnd];
//        if (!isNum) {
//            return NO;
//        }
//        int sumValue = 0;
//        
//        for (int i =0; i<14; i++) {
//            sumValue+=[[cardNo substringWithRange:NSMakeRange(i , 1) ] intValue]* [[codeArray objectAtIndex:i] intValue];
//        }
//        
//        NSString* strlast = [checkCodeDic objectForKey:[NSString stringWithFormat:@"%d",sumValue%11]];
//        
//        if ([strlast isEqualToString: [[cardNo substringWithRange:NSMakeRange(14, 1)]uppercaseString]]) {
//            return YES;
//        }
//        return  NO;
        return YES;
    }
    return NO;
    
}


+(BOOL)isDate6:(NSString *)sDate{
    NSString *preStr=@"^[0-9]{6}$";
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",preStr];
    if(![pre evaluateWithObject:sDate]){
        return NO;
    }
    int year,month;
    year=[[sDate substringWithRange:NSMakeRange(0, 4)] intValue];
    month=[[sDate substringWithRange:NSMakeRange(4, 2)] intValue];
    if(year<1700||year>2500)
        return NO;
    if(month<1||month>12)
        return NO;
    return YES;
}

+(BOOL)isDate8:(NSString *)sDate{
    NSString *preStr=@"^[0-9]{8}$";
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",preStr];
    if(![pre evaluateWithObject:sDate]){
        return NO;
    }
    int year,month,day;
    year=[[sDate substringWithRange:NSMakeRange(0, 4)] intValue];
    month=[[sDate substringWithRange:NSMakeRange(4, 2)] intValue];
    day=[[sDate substringWithRange:NSMakeRange(6, 2)] intValue];
    int iaMonthDays[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if(year<1700||year>2500)
        return NO;
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
        iaMonthDays[1] = 29;
    if (month < 1 || month > 12)
        return NO;
    if (day < 1 || day > iaMonthDays[month - 1])
        return false;
    return YES;
}

+(NSString *)md5:(NSString *)str
{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ]; 
}
+(AFHTTPRequestOperation *)requestDic:(NSDictionary *)parameters andRusult:(CSResultBlock)result andError:(CSResultBlock)errorResult
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFJSONRequestSerializer serializer];
//    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
//    //
//    securityPolicy.allowInvalidCertificates = YES;
//    manager.securityPolicy = securityPolicy;
    return [manager POST:localHost parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        if ([dic isKindOfClass:[NSDictionary class]]) {
            result(dic);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSDictionary *dic = error.userInfo;
//        NSString *localizedDescription = error.localizedDescription;
//        NSData *data = dic[@"com.alamofire.serialization.response.error.data"];
//        if (data) {
//            
//            NSDictionary *dic1 = [NSJSONSerialization JSONObjectWithData:data
//                                                                 options:NSJSONReadingMutableContainers
//                                                                   error:nil];
//            if (dic) {
//                result1(dic1);
//            }else
//            {
//                [SVProgressHUD showErrorWithStatus:@"网络错误" maskType:SVProgressHUDMaskTypeBlack];
//            }
//            
//        }else
//        {
//            [SVProgressHUD showErrorWithStatus:@"网络超时" maskType:SVProgressHUDMaskTypeBlack];
//        }
        
        
    }];
}

+(void)showAlert:(NSString *)str
{
    UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertV show];
}
@end
